package com.example.apple.baseadapter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ListView mainlv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainlv= (ListView) findViewById(R.id.main_lv);
        /**
         * 初始化数据
         * */
        List<Person> persons=new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            Person person =new Person(R.mipmap.ic_launcher,"name_"+i);
            persons.add(person);
        }
        /**
         * 创建Adapter,数据类是persons
         * */
        MainAdapter mainAdapter=new MainAdapter(persons);
        mainlv.setAdapter(mainAdapter);
        /**
         * 点击一个行布局 设置监听
         * */
        mainlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,"点击的位置是"+position,Toast.LENGTH_SHORT).show();
            }
        });
/**
 * 长按事件 设置监听
 */
        mainlv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,"长按的位置是"+position,Toast.LENGTH_SHORT).show();
                return false;
            }
        });

    }
}
